package chapter_3.message.entity;

import java.util.Objects;
import java.util.Random;

/**
 * 候选人实体
 * @author fulv
 */
public class Candidate {

    /**
     * id的范围为[0,1000],随机生成
     */
    private Integer id;
    private Integer voteNumber;

    public Candidate(){
        Random rand = new Random();
        this.id =  rand.nextInt(10001);
        this.voteNumber = 0;
    }

    public Candidate(Integer id,Integer voteNumber){
        if(id<0||id>1000){
            throw new IllegalArgumentException("候选人编号超出范围");
        }
        if(voteNumber<0){
            throw new IllegalArgumentException("初始票数不能小于0");
        }
        this.id = id;
        this.voteNumber = voteNumber;
    }

    /**
     * 投票数加一
     * @return 加一后，候选人的得票总数
     */
    public Integer addOneVote(){
        this.voteNumber = this.voteNumber + 1;
        return this.voteNumber;
    }
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getVoteNumber() {
        return voteNumber;
    }

    public void setVoteNumber(Integer voteNumber) {
        this.voteNumber = voteNumber;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id,voteNumber);
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }
        if(obj == this){
            return true;
        }
        if(obj instanceof Candidate){
            return id.equals(((Candidate)obj).getId())
                    && voteNumber.equals(((Candidate)obj).getVoteNumber());
        }
        return false;
    }
}
